/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.constant.PostIdConstant;
import org.springblade.common.constant.RoleIdConstant;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.*;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.modules.system.entity.UserOauth;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.modules.system.mapper.UserMapper;
import org.springblade.modules.system.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
@AllArgsConstructor
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {
    private static final String GUEST_NAME = "guest";
    private static final String MINUS_ONE = "-1";

    private IDeptService deptService;
    private IPostService postService;
    private IRoleService roleService;
    private IUserOauthService userOauthService;
    private ITenantService tenantService;

    @Override
    public boolean submit(User user) {
        if (Func.isNotEmpty(user.getPassword())) {
            user.setPassword(DigestUtil.encrypt(user.getPassword()));
        }
        Integer cnt = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, user.getTenantId()).eq(User::getAccount, user.getAccount()));
        if (cnt > 0) {
            throw new ApiException("当前用户已存在!");
        }
        return saveOrUpdate(user);
    }

    @Override
    public boolean addUser(User user) {
        if (Func.isNotEmpty(user.getPassword())) {
            user.setPassword(DigestUtil.sha(user.getPassword()));
        }
        Integer cnt = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getAccount, user.getAccount()));
        if (cnt > 0) {
            throw new ApiException("当前用户已存在!");
        }
        Integer count = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getPhone, user.getPhone()));
        if (count > 0) {
            throw new ApiException("当前手机号已被注册!");
        }
        user.setRoleId(RoleIdConstant.USER_ROLE);
        user.setPostId(PostIdConstant.USER_POST);
        user.setDeptId(RoleIdConstant.DEPT_ID);
        user.setCreateUser(Func.toLong(RoleIdConstant.ADMIN_ID));
        user.setUpdateUser(Func.toLong(RoleIdConstant.ADMIN_ID));
        return saveOrUpdate(user);
    }

    @Override
    public IPage<User> selectUserPage(IPage<User> page, User user) {
        return page.setRecords(baseMapper.selectUserPage(page, user));
    }

    @Override
    public UserInfo userInfo(Long userId) {
        UserInfo userInfo = new UserInfo();
        User user = baseMapper.selectById(userId);
        userInfo.setUser(user);
        if (Func.isNotEmpty(user)) {
            List<String> roleAlias = baseMapper.getRoleAlias(Func.toStrArray(user.getRoleId()));
            userInfo.setRoles(roleAlias);
        }
        return userInfo;
    }

    @Override
    public UserInfo userInfo(String tenantId, String account, String password) {
        UserInfo userInfo = new UserInfo();
        User user = baseMapper.getUser(tenantId, account, password);
        userInfo.setUser(user);
        if (Func.isNotEmpty(user)) {
            List<String> roleAlias = baseMapper.getRoleAlias(Func.toStrArray(user.getRoleId()));
            userInfo.setRoles(roleAlias);
        }
        return userInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInfo userInfo(UserOauth userOauth) {
        UserOauth uo = userOauthService.getOne(Wrappers.<UserOauth>query().lambda().eq(UserOauth::getUuid, userOauth.getUuid()).eq(UserOauth::getSource, userOauth.getSource()));
        UserInfo userInfo;
        if (Func.isNotEmpty(uo) && Func.isNotEmpty(uo.getUserId())) {
            userInfo = this.userInfo(uo.getUserId());
            userInfo.setOauthId(Func.toStr(uo.getId()));
        } else {
            userInfo = new UserInfo();
            if (Func.isEmpty(uo)) {
                userOauthService.save(userOauth);
                userInfo.setOauthId(Func.toStr(userOauth.getId()));
            } else {
                userInfo.setOauthId(Func.toStr(uo.getId()));
            }
            User user = new User();
            user.setAccount(userOauth.getUsername());
            userInfo.setUser(user);
            userInfo.setRoles(Collections.singletonList(GUEST_NAME));
        }
        return userInfo;
    }

    @Override
    public boolean grant(String userIds, String roleIds) {
        User user = new User();
        user.setRoleId(roleIds);
        return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
    }

    @Override
    public boolean resetPassword(String userIds) {
        User user = new User();
        user.setPassword(DigestUtil.encrypt(CommonConstant.DEFAULT_PASSWORD));
        user.setUpdateTime(DateUtil.now());
        return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
    }

    @Override
    public boolean updatePassword(Long userId, String oldPassword, String newPassword, String newPassword1) {
        User user = getById(userId);
        if (!newPassword.equals(newPassword1)) {
            throw new ServiceException("请输入正确的确认密码!");
        }
        if (!user.getPassword().equals(DigestUtil.encrypt(oldPassword))) {
            throw new ServiceException("原密码不正确!");
        }
        return this.update(Wrappers.<User>update().lambda().set(User::getPassword, DigestUtil.encrypt(newPassword)).eq(User::getId, userId));
    }

    @Override
    public List<String> getRoleName(String roleIds) {
        return baseMapper.getRoleName(Func.toStrArray(roleIds));
    }

    @Override
    public List<String> getDeptName(String deptIds) {
        return baseMapper.getDeptName(Func.toStrArray(deptIds));
    }

    @Override
    public void importUser(List<UserExcel> data) {
        data.forEach(userExcel -> {
            User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class));
            // 设置部门ID
            user.setDeptId(deptService.getDeptIds(userExcel.getTenantId(), userExcel.getDeptName()));
            // 设置岗位ID
            user.setPostId(postService.getPostIds(userExcel.getTenantId(), userExcel.getPostName()));
            // 设置角色ID
            user.setRoleId(roleService.getRoleIds(userExcel.getTenantId(), userExcel.getRoleName()));
            // 设置默认密码
            user.setPassword(CommonConstant.DEFAULT_PASSWORD);
            this.submit(user);
        });
    }

    @Override
    public List<UserExcel> exportUser(Wrapper<User> queryWrapper) {
        List<UserExcel> userList = baseMapper.exportUser(queryWrapper);
        userList.forEach(user -> {
            user.setRoleName(StringUtil.join(roleService.getRoleNames(user.getRoleId())));
            user.setDeptName(StringUtil.join(deptService.getDeptNames(user.getDeptId())));
            user.setPostName(StringUtil.join(postService.getPostNames(user.getPostId())));
        });
        return userList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean registerGuest(User user, Long oauthId) {
        Tenant tenant = tenantService.getOne(Wrappers.<Tenant>lambdaQuery().eq(Tenant::getTenantId, user.getTenantId()));
        if (tenant == null || tenant.getId() == null) {
            throw new ApiException("租户信息错误!");
        }
        UserOauth userOauth = userOauthService.getById(oauthId);
        if (userOauth == null || userOauth.getId() == null) {
            throw new ApiException("第三方登陆信息错误!");
        }
        user.setRealName(user.getName());
        user.setAvatar(userOauth.getAvatar());
        user.setRoleId(MINUS_ONE);
        user.setDeptId(MINUS_ONE);
        user.setPostId(MINUS_ONE);
        boolean userTemp = this.submit(user);
        userOauth.setUserId(user.getId());
        userOauth.setTenantId(user.getTenantId());
        boolean oauthTemp = userOauthService.updateById(userOauth);
        return (userTemp && oauthTemp);
    }


}
